<?php
namespace App\Process;
use App\Pool\MysqlPool;//载入mysqlpool
use EasySwoole\EasySwoole\ServerManager;
class QuickTask extends \EasySwoole\EasySwoole\Swoole\Task\AbstractAsyncTask
{
/**
     * 执行任务的内容
     * @param mixed $taskData     任务数据
     * @param int   $taskId       执行任务的task编号
     * @param int   $fromWorkerId 派发任务的worker进程号
     * @author : evalor <master@evalor.cn>
     */
    function run($taskData, $taskId, $fromWorkerId,$flags = null)
    {
        // 需要注意的是task编号并不是绝对唯一
        // 每个worker进程的编号都是从0开始
        // 所以 $fromWorkerId + $taskId 才是绝对唯一的编号
        // !!! 任务完成需要 return 结果
      //Index::message_offsend($taskData['userid'],$taskData['fd']);
        $dbs = MysqlPool::defer();
       	$server = ServerManager::getInstance()->getSwooleServer();
        $str = "select * from message_offline where FIND_IN_SET('".$taskData['userid']."',userset) "; //and channelType ='0'
        $result = $dbs->rawQuery($str);
        if(empty($result)){return false;}
     	  $failSendList = array();
        foreach($result as $k => $v){
          sleep(1);
         
          $id = $server->push($taskData['fd'],json_encode(array('content'=>$v['content'],'unreadCount'=>count($result))));
          if($id){array_push($failSendList,$v['id']);}
        }
        return $failSendList;
    }

    /**
     * 任务执行完的回调
     * @param mixed $result  任务执行完成返回的结果
     * @param int   $task_id 执行任务的task编号
     * @author : evalor <master@evalor.cn>
     */
    function finish($result, $task_id)
    {
      if($result){
        // 任务执行完的处理
        //进行离线数据库删除操作
        $dbs = MysqlPool::defer();
        $str = "delete from message_offline where id = " . implode(' or id=',$result);
        $dbs->rawQuery($str);
      }
    }
}



?>